package com.ruoyi.category.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.category.domain.CategoryWithProductsDTO;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.goods.domain.Goods;
import com.ruoyi.merchant.domain.Merchant;
import com.ruoyi.merchant.service.IMerchantService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.category.domain.GoodsCategory;
import com.ruoyi.category.service.IGoodsCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 商品分类信息Controller
 * 
 * @author ruoyi
 * @date 2025-06-01
 */
//@Anonymous()
@RestController
@RequestMapping("/ruoyi/category")
public class GoodsCategoryController extends BaseController
{
    @Autowired
    private IGoodsCategoryService goodsCategoryService;

    @Autowired
    private IMerchantService merchantService;

//    功能：获取所有商品分类及其对应的所有商品信息
//    特点：不带参数，查询所有商家的商品数据
//    使用场景：
//    管理员查看所有商品分类和商品信息
//    小程序端所有用户查看商品（不区分商家）
//    不需要筛选特定商家的场景
    /**
     * 获取商品分类及对应商品列表(前端所需结构)
     */
    @GetMapping("/categoryWithProducts")
    public AjaxResult getCategoryWithProducts() {
        // 获取当前用户所属商家ID
        Long userId = getUserId();
        Merchant merchantInfo = merchantService.selectMerchantByUserId(userId);
        Long merchantId = merchantInfo != null ? merchantInfo.getMerchantId() : null;

        // 如果是管理员用户，merchantId为null，让服务层处理所有商家数据
        // 如果是非商家用户，也允许查看（与商品管理逻辑保持一致）

        // 非商家用户返回空数据
//        if (merchantId == null) {
//            return AjaxResult.success(new ArrayList<>());
//        }

        List<CategoryWithProductsDTO> result = goodsCategoryService.getCategoryWithProducts(merchantId);
        return AjaxResult.success(result);
    }


    /**
     * 查询商品分类信息列表
     */
    // 第一步 注释约束
    // @PreAuthorize("@ss.hasPermi('ruoyi:category:list')")
    // 第二步 添加格式转换
    @GetMapping(value = "/list", produces = "application/json;charset=UTF-8")
    public AjaxResult list(GoodsCategory goodsCategory)
    {
        List<GoodsCategory> list = goodsCategoryService.selectGoodsCategoryList(goodsCategory);
        return success(list);
    }

    /**
     * 导出商品分类信息列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:export')")
    @Log(title = "商品分类信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsCategory goodsCategory)
    {
        List<GoodsCategory> list = goodsCategoryService.selectGoodsCategoryList(goodsCategory);
        ExcelUtil<GoodsCategory> util = new ExcelUtil<GoodsCategory>(GoodsCategory.class);
        util.exportExcel(response, list, "商品分类信息数据");
    }

    /**
     * 获取商品分类信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(goodsCategoryService.selectGoodsCategoryByCategoryId(categoryId));
    }

    /**
     * 新增商品分类信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:add')")
    @Log(title = "商品分类信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsCategory goodsCategory)
    {
        return toAjax(goodsCategoryService.insertGoodsCategory(goodsCategory));
    }

    /**
     * 修改商品分类信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:edit')")
    @Log(title = "商品分类信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsCategory goodsCategory)
    {
        return toAjax(goodsCategoryService.updateGoodsCategory(goodsCategory));
    }

    /**
     * 删除商品分类信息
     */
    @PreAuthorize("@ss.hasPermi('ruoyi:category:remove')")
    @Log(title = "商品分类信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        return toAjax(goodsCategoryService.deleteGoodsCategoryByCategoryIds(categoryIds));
    }

    /**
     * 获取商品分类信息树列表
     * @return
     */
    @GetMapping("/selectCategoryList")
    public AjaxResult selectCategoryList()
    {
    	List<GoodsCategory> list = goodsCategoryService.selectCategoryList();
    	return AjaxResult.success(list);
    }





}
